package com.yunji.system.config.mybatis;

import com.google.common.collect.HashBasedTable;
import com.google.common.collect.Sets;
import com.google.common.collect.Table;
import com.yunji.common.enums.common.PermissionTypeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.sql.DataSource;
import java.sql.Connection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @author : peter-zhu
 * @date : 2024/11/26 11:24
 * @description : TODO
 **/
@Component
public class SqlParseCacheManager {
    //    private static final Cache<String, Object> CACHE = CacheBuilder.newBuilder()
//            .maximumSize(1000)
//            .build();
    private static final Table<String/*tableName*/, String/*columnName*/, Boolean/*true*/> table = HashBasedTable.create();

    @Autowired
    private NamedParameterJdbcTemplate jdbcTemplate;

    @Autowired
    private DataSource dataSource;


    @PostConstruct
    public void initializeCache() {
        String sql = "SELECT table_name,column_name FROM information_schema.COLUMNS" +
                " WHERE table_schema = :databaseName AND column_name in (:columns)" +
                " GROUP BY table_name,column_name";
        Set<String> columns = Sets.newHashSet("creator");
        PermissionTypeEnum[] values = PermissionTypeEnum.values();
        for (PermissionTypeEnum value : values) {
            columns.addAll(value.getColumns());
        }
        Map<String, Object> params = new HashMap<>();
        params.put("databaseName", getDatabaseName());
        params.put("columns", columns);
        jdbcTemplate.query(sql, params, rs -> {
            System.out.println("excute sql --------" + sql);
            synchronized (table) {
                while (rs.next()) {
                    table.put(rs.getString("table_name"), rs.getString("column_name"), true);
                }
            }
        });
    }

    private String getDatabaseName() {
        try (Connection connection = dataSource.getConnection()) {
            String databaseName = null;
            String sql = "SELECT DATABASE()";
            databaseName = jdbcTemplate.queryForObject(sql, Collections.emptyMap(), String.class);
            return databaseName;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static Boolean exists(String tableName, String columnName) {
        Boolean value = getValue(tableName, columnName);
        return value != null ? value : false;
    }

    public static Boolean getValue(String tableName, String columnName) {
        synchronized (table) {
            return table.get(tableName, columnName);
        }
    }

    public static Map<String, Boolean> getColumn(String column) {
        synchronized (table) {
            return table.column(column);
        }
    }

    public static Map<String, Boolean> getRow(String tableName) {
        synchronized (table) {
            return table.row(tableName);
        }
    }

    public static Map<String, Map<String, Boolean>> getAllValues() {
        synchronized (table) {
            return table.rowMap();
        }
    }

}
